﻿// ***********************************************************************
// Project          : Micua
// Assembly         : Micua.UI
// Author           : Administrator
// Created          : 2013-12-29 6:20 PM
// 
// Last Modified By : Administrator
// Last Modified On : 2013-12-29 6:20 PM
// ***********************************************************************
// <copyright file="Gloabl.cs" company="Wedn.Net">
//     Copyright (c) Wedn.Net. All rights reserved.
// </copyright>
// <summary></summary>
// ***********************************************************************

using System;
using System.Web.Http;
using System.Web.Mvc;
using System.Web.Optimization;
using System.Web.Routing;
using Micua.Core.IoC;
using Micua.Core.Routing;
using Micua.Core.Tracing;
using Micua.UI.Config;

namespace Micua.UI
{
    /// <summary>
    /// 网站应用程序全局类型
    /// </summary>
    /// <remarks>
    ///  2013-11-23 22:33 Created By iceStone
    /// </remarks>
    public class MicuaApp : System.Web.HttpApplication
    {
        /// <summary>
        /// 网站程序开始事件
        /// </summary>
        /// <remarks>
        ///  2013-11-23 22:33 Created By iceStone
        /// </remarks>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="EventArgs"/> instance containing the event data.</param>
        protected virtual void Application_Start(object sender, EventArgs e)
        {
            //MicuaMvc.Init();
            //清除MVC的响应标头
            //MvcHandler.DisableMvcResponseHeader = true;

            //初始化IoC
            IoCHelper.InitializeWith(new DependencyResolverFactory());

            //加载全局设置
            try
            {
                //Setting.Init(IoCHelper.Resolve<IOptionService>().GetSetting());//ServiceFactory.ServiceFactory.Instance.OptionService.GetSetting());
            }
            catch (System.Exception exception)
            {
                LogHelper.WriteErrorLog("MicuaMvc", "Exception", exception);
                //throw;
            }

            //注册自定义的控制器，使MVC控制器可以支持依赖注入
            ControllerBuilder.Current.SetControllerFactory(new ResolverControllerFactory().GetControllerFactory());

            //注册区域信息
            AreaRegistration.RegisterAllAreas();

            //注册WebAPI
            WebApiConfig.Register(GlobalConfiguration.Configuration);

            //注册路由
            RouteConfig.RegisterRoutes(RouteTable.Routes);

            //注册控制器配置
            //ControllerBuilder.Current.RegisterDefaultNamespace();

            //注册过滤器
            FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);

            //注册捆绑资源
            BundleConfig.RegisterBundles(BundleTable.Bundles);

            //注册模版引擎
            //ViewEngineConfig.RegisterViewEngines();


            //开始统计
            //var thread = new Thread(() =>
            //{
            //    while (true)
            //    {
            //        Thread.Sleep(Common.Config.StatisticsTimespan.ToInt(300000));
            //        //更新流量次数
            //        ServiceFactory.ServiceFactory.Instance.OptionService.UpdateValue("site_view_count", Setting.SiteViewCount.ToString());
            //        ServiceFactory.ServiceFactory.Instance.OptionService.SaveChanges();
            //    }
            //});
            //thread.Start();
        }

        /// <summary>
        /// 会话开始事件
        /// </summary>
        /// <remarks>
        ///  2013-11-23 22:33 Created By iceStone
        /// </remarks>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="EventArgs"/> instance containing the event data.</param>
        protected virtual void Session_Start(object sender, EventArgs e)
        {

        }

        /// <summary>
        /// 请求开始时间
        /// </summary>
        /// <remarks>
        ///  2013-11-23 22:33 Created By iceStone
        /// </remarks>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="EventArgs"/> instance containing the event data.</param>
        protected virtual void Application_BeginRequest(object sender, EventArgs e)
        {
            //WednApplication.Init();
        }

        /// <summary>
        /// 验证请求
        /// </summary>
        /// <remarks>
        ///  2013-11-23 22:33 Created By iceStone
        /// </remarks>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="EventArgs"/> instance containing the event data.</param>
        protected virtual void Application_AuthenticateRequest(object sender, EventArgs e)
        {

        }

        /// <summary>
        /// 会话结束事件
        /// </summary>
        /// <remarks>
        ///  2013-11-23 22:33 Created By iceStone
        /// </remarks>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="EventArgs"/> instance containing the event data.</param>
        protected virtual void Session_End(object sender, EventArgs e)
        {

        }

        /// <summary>
        /// 程序结束事件
        /// </summary>
        /// <remarks>
        ///  2013-11-23 22:33 Created By iceStone
        /// </remarks>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="EventArgs"/> instance containing the event data.</param>
        protected virtual void Application_End(object sender, EventArgs e)
        {

        }

        /// <summary>
        /// 程序错误事件
        /// </summary>
        /// <remarks>
        ///  2013-11-23 22:33 Created By iceStone
        /// </remarks>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="EventArgs"/> instance containing the event data.</param>
        protected virtual void Application_Error(object sender, EventArgs e)
        {

        }
    }
}